package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.JSONException;

/**
 *
 * @Title: apiWorkOrderStaff
 * @Description:深圳宝安查询人员信息接口--外部接口
 * @author: chieh
 * @date 2021-07-13 15:45:00
 *
 */
public class apiWorkOrderStaff implements ServiceBackInvoke {
	private static final Log log = LogFactory.getLog(apiWorkOrderStaff.class);

	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		
		log.debug("-------------------------start apiWorkOrderStaff 开始-----------------");
		
		long startTime = System.currentTimeMillis();
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("utf-8");
		String reJson = null;
		long endTime = 0;
		
		//调用处理信息
		def sys_source = request.getParameter("sys_source");//来源系统
		String sys_flag = request.getParameter("sys_flag");//同步标识

		log.debug("解析信息-sys_source：" + sys_source+"	sys_flag" + sys_flag);
		
		String staffid = "";
		String staffname = "";
		String rolegroup = "";
		String area = "";
		String workstatus = "";
		String createby = "";
		String createddate = "";
		
		JSONArray resArray = new JSONArray();
		JSONObject resObject = new JSONObject();
				
		try {
			if (sys_source != "BA"&&sys_flag != "update") {
				endTime = System.currentTimeMillis();
				reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(校验字段失败;),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
				return reJson;
			}
			def stafflistresult = eliteBean.chaxun_sql("select s.LOGINNAME as staffid,s.STAFFNAME as staffname,rg.ROLEGROUPDESCRIPTION as rolegroup,g.Area_desc as area,s.WORKSTATUS as workstatus,s1.STAFFNAME as createby,s.CREATEDDATE as createddate from  staff s left join ROLEGROUP rg on s.DEFAULTROLEGROUP=rg.ROLEGROUP_ID left join RolegroupArea rga on rga.Group_id=rg.ROLEGROUP_ID LEFT JOIN staff s1 on s1.STAFF_ID=s.CREATEDBY LEFT JOIN Group_Area g on g.Area_guid=rga.Area_guid ", "dataSource");//where g.Area_guid='74BCB8CD-D886-7B4D-BE0E-AAECE71FA6DE'
			log.debug("查看宝安员工是否存在sql结果:" + stafflistresult);
			int count=stafflistresult.size();
			if(count > 0) {
				for (int i = 0; i < count; i++) {
					staffid = stafflistresult.get(i).get("staffid");
					staffname = stafflistresult.get(i).get("staffname");
					rolegroup = stafflistresult.get(i).get("rolegroup");
					area = stafflistresult.get(i).get("area");
					workstatus = stafflistresult.get(i).get("workstatus");
					createby = stafflistresult.get(i).get("createby");
					createddate = stafflistresult.get(i).get("createddate");
							
		
					resObject.put("staffid", staffid);
					resObject.put("staffname", staffname);
					resObject.put("rolegroup", rolegroup);
					resObject.put("area", area);
					resObject.put("workstatus", workstatus);
					resObject.put("createby", createby);
					resObject.put("createddate", createddate);
					resArray.add(resObject);
				}
					endTime = System.currentTimeMillis();
					reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"查询接口调用成功,调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":["+resArray.toString()+"]}";
				
			}else{
				endTime = System.currentTimeMillis();
				reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"查询接口调用成功(无宝安相关人员),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
				return reJson;
			}
		} catch (Exception e) {
			log.error("接口处理异常" + e.getMessage());
			// 失败返回
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"查询接口调用处理失败(" + e.getMessage() + ")\",\"data\":[]}";

		}
		return reJson;

	}
	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		log.debug("解析-k=" + getKey + " v=" + Rev_Value);
		return Rev_Value;
	}
}